package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.imau.bean.Brand;
import com.imau.bean.Page;
import com.imau.common.BaseException;
import com.imau.common.ResultCode;
import com.imau.service.BrandService;
import com.imau.service.impl.BrandServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能模块请求统一处理:反射
 *
 *  /brand/add
 *  /brand/delete
 */
@WebServlet("/brand/*")
public class BrandController extends BaseController {

    //服务
    private BrandService service = new BrandServiceImpl();


    private void addOrUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException,BaseException {

        if(true)
        {
            throw new BaseException(ResultCode.UNAUTHENTICATED);
        }


        //1.获取JSON串
        String jsonString = request.getReader().readLine();
        //2.转成java对象
        Brand brand = JSON.parseObject(jsonString, Brand.class);

        System.out.println(brand);
        //3.调用服务添加
        boolean flag;
        if(brand.getId()!=null)
        {

            flag= service.edit(brand);
        }else {

           flag= service.add(brand);
        }


        //4.响应结果
        response.getWriter().write("OK");
    }

    private void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");

        //获取查询条件
        String jsonBrand = request.getReader().readLine();


        Brand brand = JSON.parseObject(jsonBrand, Brand.class);


        //数据准备
        Page<Brand> brandPage = service.queryBypageAndCondition(Long.valueOf(currentPage), Long.valueOf(pageSize), brand);

        //数据转成JSON
        String jsonString = JSON.toJSONString(brandPage);

        //响应到前端

        response.getWriter().write(jsonString);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        //获取id
        String id = request.getParameter("id");

        boolean delete = service.delete(Integer.valueOf(id));

        if(delete)
        {
            response.getWriter().write("OK");

        }else {

            response.getWriter().write("ERROR");
        }
    }


    private void batchDelete(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        //获取前端就Id数组
        String idsStr = request.getReader().readLine();




        Integer[] ids = JSON.parseObject(idsStr, Integer[].class);


        //调用服务层


        boolean delete = service.deleteBatchByIds(ids);

        if(delete)
        {
            response.getWriter().write("OK");

        }else {

            response.getWriter().write("ERROR");
        }
    }

    private void get(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String id = request.getParameter("id");


        Brand brand = service.getById(Integer.valueOf(id));

        response.getWriter().write(JSON.toJSONString(brand));
    }

}
